int searchFirstTargetIndex(int* nums, int numsSize, int target) {
    int left = 0;
    int right = numsSize - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (target == nums[mid]) {
            if (mid == 0 || nums[mid - 1] < target) return mid;
            else right = mid - 1;
        } else if (target < nums[mid]) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return -1;
}

int searchLastTargetIndex(int* nums, int numsSize, int target) {
    int len = numsSize;
    int left = 0;
    int right = len - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (target == nums[mid]) {
            if (mid == len - 1 || nums[mid + 1] > target) return mid;
            else left = mid + 1;
        } else if (target < nums[mid]) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return -1;
}

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* searchRange(int* nums, int numsSize, int target, int* returnSize) {
    int* result = (int *)malloc(2 * sizeof(int));
    *returnSize = 2;
     if (nums == NULL || numsSize == 0) {
        result[0] = -1;
        result[1] = -1;
        return result;
    }

    int firstTargetIndex = searchFirstTargetIndex(nums, numsSize, target);
    if (firstTargetIndex == -1) {
        result[0] = -1;
        result[1] = -1;
    } else {
        int lastTargetIndex = searchLastTargetIndex(nums, numsSize, target);
        result[0] = firstTargetIndex;
        result[1] = lastTargetIndex;
    }

    return result;
}